﻿using System;
using System.Threading;

namespace YQHL.GS
{
    public class App
    {
        public App()
        {
        }

        /// <summary>
        /// 进程锁（防止线程竞争）
        /// </summary>
        private object AppLock = new object();

        private Thread AppThread;

        /// <summary>
        /// 启动
        /// </summary>
        public void Run()
        {
            if (GetState != 1)
            {
                lock (AppLock)
                {
                    AppThread = new Thread(new ThreadStart(delegate ()
                    {
                        while (true)
                        {
                            try
                            {
                                BankEvent();
                            }
                            catch (Exception ex)
                            {
                                //throw;
                            }
                            Thread.Sleep(10 * 60 * 1000);

                            /*
                            foreach (var item in AccountArry)
                            {
                                var server = new YQHL.GS.AppServer();
                                var query = new YQHL.GS.Model.PageQuery();
                                query.AccNo = item;
                                query.BeginDate = DateTime.Now.AddDays(-1);
                                query.EndDate = DateTime.Now;
                                query.MaxAmt = 999999999999 / 100;
                                query.MinAmt = 1 / 100;
                                query.NextTag = "";
                                query.ReqReserved1 = "";
                                query.ReqReserved2 = "";
                                var data = server.GetBankData(query);
                                BankAction(data);
                            }*/
                        }
                    }));
                    AppThread.Start();
                }
            }
        }

        /// <summary>
        /// 停止(可能会引发进程异常或者数据库锁死)
        /// </summary>
        public void Stop()
        {
            if (GetState == 1)
            {
                lock (AppLock)
                {
                    try
                    {
                        //终止线程
                        AppThread.Abort();
                    }
                    catch (Exception)
                    {
                        //throw;
                    }
                    AppThread = null;
                }
            }
        }

        /// <summary>
        /// 获取当前服务运行状态：0.已停止, 1.正在运行
        /// </summary>
        public int GetState
        {
            get
            {
                lock (AppLock)
                {
                    if (AppThread == null)
                    {
                        return 0;
                    }
                    if (AppThread.ThreadState == ThreadState.Running)
                    {
                        return 1;
                    }
                    return 0;
                }
            }
        }

        public Action BankEvent
        {
            get
            {
                lock (AppLock)
                {
                    return bankEvent;
                }
            }
            set
            {
                lock (AppLock)
                {
                    bankEvent = value;
                }
            }
        }

        private Action bankEvent;
    }
}